import { defineStore } from "pinia";
import { loginApi } from "@/api/user"; //api
//写法1：选项式
//写法2：组合式

const useUserStore = defineStore("desr", {
  //options
  state: () => ({
    token: "",
  }),
  //方法：同步和异步
  actions: {
    //1.发送请求获取到数据 存储token
    async getUserToken({ username, password }) {
      try {
        const res = await loginApi({ username, password });
        //储存token
        this.token = res.access_token;
        console.log(this.token);
        //本地存储

        return Promise.resolve(true);
      } catch (error) {
        return Promise.reject(false);
      }
    },
  },
  //  计算属性
  getters: {},
  persist: {
    enabled: true,
  },
});

export default useUserStore;
